package cn.sea;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DragonSea
 * on 2018-11-07
 */
//列出网站所有文件
public class ListFileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           String path=this.getServletContext().getRealPath("/upload");
           Map map= new HashMap();
           listfile(new File(path),map);
           request.setAttribute("map",map);
           request.getRequestDispatcher("/listfile.jsp").forward(request,response);
    }
    //保存递归出来的资源
    public void listfile(File file,Map map){
        if(!file.isFile()){
            File children[]=file.listFiles();
            for(File f: children){
                listfile(f,map);
            }
        }else{
            String fileName=file.getName().substring(file.getName().indexOf("_")+1);
             map.put(file.getName(),fileName);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
    }
}
